package dnl.toolbox.sys;

import dnl.jexem.cmd.CommandExecution;
import dnl.jexem.cmd.CommandExecutionFactory;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 
 * @author Daniel Orr
 * 
 */
public class EnvironmentUtils {

	/**
	 * checks if the given command exist on default path.
	 * 
	 * @param cmd
	 * @return
	 */
	public static boolean checkIfCommandExists(String cmd) {
		CommandExecution commandExecution = CommandExecutionFactory.createLocalCommandExecution("which", cmd);
		// a 1 second timeout
		commandExecution.executeCommand(0);
		// if we had errors
		if (commandExecution.getExitStatus() != 0) {
			return false;
		} else {
			return true;
		}
	}

    public static void printEnv(){
        Map<String, String> env = System.getenv();
        List<String> vars = new ArrayList(env.keySet());
        Collections.sort(vars);
        for (String envVariable: vars) {
            System.out.println(envVariable+"="+env.get(envVariable));
        }
    }

}
